(function (window, undefined) {
  let timer = null;
  function Bullet(option) {
    //参数判断，避免传入错误参数
    ops = option instanceof Object ? option : {};
    this.width = ops.width || 20;
    this.height = ops.height || 20;
    this.radius = ops.redius || 50;
    this.color = ops.color || "blue";
    this.x = ops.x || 0;
    this.y = ops.y || 0;
    this.abso = "absolute";
    this.direction = ops.direction || "right";
    this.isBelong = ops.isBelong || "NPC";
    //存放生成的子弹实例
    this.bulletElements = [];
  }
  //生成子弹
  Bullet.prototype.render = function (map) {
    let div = document.createElement("div");
    div.style.width = this.width + "px";
    div.style.height = this.height + "px";
    div.style.borderRadius = this.radius + "%";
    div.style.backgroundColor = this.color;
    div.style.position = this.abso;
    div.style.left = this.x + "px";
    div.style.top = this.y + "px";
    div.customDirection = this.direction;
    div.customBelong = this.isBelong;
    map.appendChild(div);
    this.bulletElements.push(div);
  };
  //子弹移动，定时移动
  Bullet.prototype.move = function (map, elementsArr) {
    let maxWidth = map.clientWidth;
    let maxHeight = map.clientHeight;
    //已有定时器，不再开启
    if (timer) return;
    // console.log(this.bulletElements);
    //子弹一旦创建，即沿着初始移动的方向知道碰到其他坦克，阵营中心，超出地图范围，则删除该子弹实体
    timer = setInterval(() => {
      for (let i = 0; i < this.bulletElements.length; i++) {
        //根据各个元素初始方向持续运动
        let item = this.bulletElements[i];
        let moveDirection = item.customDirection;
        switch (moveDirection) {
          case "left":
            item.style.left = item.offsetLeft - 20 + "px";
            break;
          case "top":
            item.style.top = item.offsetTop - 20 + "px";
            break;
          case "right":
            item.style.left = item.offsetLeft + 20 + "px";
            break;
          case "bottom":
            item.style.top = item.offsetTop + 20 + "px";
            break;
          default:
            break;
        }
        if (item.offsetLeft < 0 || item.offsetLeft >= maxWidth || item.offsetTop < 0 || item.offsetTop >= maxHeight) {
          map.removeChild(item);
          //将运动出局的子弹先置空，避免改变元素顺序，后续进行清除
          this.bulletElements[i] = null;
          // this.bulletElements.splice(i, 1);
        }
        for (let item of elementsArr) {
          this.judgFired(item, this.bulletElements[i]);
        }
      }
      //将出局置空元素清除
      this.bulletElements = this.bulletElements.filter((m) => m !== null);
      if (this.bulletElements.length === 0) {
        clearInterval(timer);
        timer = null;
      }
    }, 150);
  };
  //子弹是否击中玩家或NPC或某个阵营
  Bullet.prototype.judgFired = function (judgObj, bullet) {
    //子弹，阵营或坦克所属相同，击中：否
    if (judgObj.customBelong === bullet.customBelong) {
      return false;
    }
    // 首先判断判断对象在地图中的所占范围
    let rangeLeft = judgObj.offsetLeft;
    let rangeRight = rangeLeft + judgObj.clientWidth;
    let rangeTop = judgObj.offsetTop;
    let rangeBottom = rangeTop + judgObj.clientHeight;
    //子弹出现在阵营或坦克实例范围中，该对象被击中， 击中：是
    if (bullet.offsetLeft > rangeLeft && bullet.offsetLeft < rangeRight && bullet.offsetTop > rangeTop && bullet.offsetTop < rangeBottom) {
      return true;
    }
    // 击中：否
    return false;
  };
  window.Bullet = Bullet;
  //
})(window, undefined);
